package com.wefans.wefansapp.activity;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.StarData;
import com.wefans.wefansapp.common.tool.data.StarGroupData;

public class MystarGroupAdapter extends BaseExpandableListAdapter {

	private ArrayList<StarGroupData> mGroupList = null;
    private LayoutInflater mInflater = null;
    private ViewHolder viewHolder = null;
    private MystarFavoriteListener mFavoriteListener = null;

	public MystarGroupAdapter(Context c, ArrayList<StarGroupData> groupList, MystarFavoriteListener favoriteListener) {
		super();
		this.mInflater = LayoutInflater.from(c);
		this.mGroupList = groupList;
		this.mFavoriteListener = favoriteListener;
	}

    
	@Override
	public int getGroupCount() {
		return mGroupList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mGroupList.get(groupPosition).getChildList().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mGroupList.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return mGroupList.get(groupPosition).getChildList().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		View v = convertView;
        
        if(v == null){
            viewHolder = new ViewHolder();
            v = mInflater.inflate(R.layout.list_item_stargroup, parent, false);
            viewHolder.tv_groupName = (TextView) v.findViewById(R.id.TV_STARTGROUP_NAME);
            v.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)v.getTag();
        }
        
        StarGroupData dtGroup = (StarGroupData) getGroup(groupPosition);
        if (dtGroup != null) {
        	viewHolder.tv_groupName.setText(dtGroup.getStrName());
        }
        return v;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		
		View v = convertView;
        
        if(v == null){
            viewHolder = new ViewHolder();
            v = mInflater.inflate(R.layout.list_item_star, null);
            viewHolder.tv_childName = (TextView) v.findViewById(R.id.TV_START_NAME);
            viewHolder.iv_favorite = (ImageView) v.findViewById(R.id.IV_FAVORITE_STATE);
            v.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)v.getTag();
        }
        
        final StarGroupData dtGroupStar = (StarGroupData) getGroup(groupPosition);
        final StarData dtStar = (StarData) getChild(groupPosition, childPosition);
        
        if (dtStar != null) {
        	viewHolder.tv_childName.setText(dtStar.getStrName());
        	viewHolder.iv_favorite.setSelected(dtStar.isFavorite());
        	viewHolder.iv_favorite.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				dtStar.setFavorite(!dtStar.isFavorite());
    				
    				if (mFavoriteListener != null) {
    					mFavoriteListener.onFavoriteChange(dtStar.getStrId(), String.format("%s-%s", dtGroupStar.getStrName(), dtStar.getStrName()), dtStar.isFavorite());
    				}
    				notifyDataSetChanged();
    			}
    		});
        }
         
        return v;

	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	
	class ViewHolder {        
        public TextView tv_groupName;
        public TextView tv_childName;
        public ImageView iv_favorite;
    }


}
